//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.controller.GetImageDataCommand;
	import reader.core.ReaderModelLocator;
	import reader.core.WindowManager;
	import reader.events.InsertPhotoEvent;
	import reader.events.ServiceEvent;
	import reader.model.IDraftProxy;
	import reader.model.IServiceProxy;
	import reader.model.vo.AlbumPhotosVO;
	import reader.model.vo.AlbumVO;
	import reader.model.vo.PhotoVO;
	import reader.model.vo.UploadPhotoVO;
	import reader.services.IGetAlbumAble;
	import reader.services.IUploadPhotoAble;
	import reader.services.ServiceFactory;
	import reader.utils.HTMLCoder;
	import reader.view.components.InsertPhotos;

	public class InsertPhotosMediator extends ServiceAbleMediator implements IMediator
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		
		public static const NAME:String = "InsertPhotosMediator";
		
		public function get insertPhotos():InsertPhotos
		{
			return this.viewComponent as InsertPhotos;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var draftProxy:IDraftProxy;
		private var serviceProxy:IServiceProxy;		
		private var getPhotoService:IGetAlbumAble;
		private var putPhotoService:IUploadPhotoAble;
		
		private var uploadPhotoVO:UploadPhotoVO;
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function InsertPhotosMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			
			facade.registerCommand(ApplicationFacade.OPEN_GET_PHOTO, GetImageDataCommand);
			
			draftProxy = facade.retrieveProxy(ReaderModelLocator.instance.sqliteProxyFactory.draftProxyName) as IDraftProxy;
			serviceProxy = facade.retrieveProxy(ReaderModelLocator.instance.sqliteProxyFactory.serviceProxyName) as IServiceProxy;
			
			insertPhotos.addEventListener(InsertPhotos.CLOSE_TYPED_WINDOW, onCloseWindow_handler, false, 0, true);
			insertPhotos.addEventListener(InsertPhotos.DOUBLE_CLICK_GET_ALBUMABLE, onGetAlbumPhoto_handler, false, 0, true);
			insertPhotos.addEventListener(InsertPhotos.GET_IMAGE_FILE, onOpenForImageChoose_handler, false, 0, true);
			insertPhotos.addEventListener(InsertPhotos.UPLOAD_PHOTO, onPostAlbumPhoto_handler, false, 0, true);
			insertPhotos.addEventListener(InsertPhotos.INSERT_SERVICE_PHOTOS, onInsertServicePhoto_handler, false, 0, true);
			insertPhotos.addEventListener(ServiceEvent.GET_ALBUM_SERVICE, onGetAlbumServiceChange_handler, false, 0, true);
			insertPhotos.addEventListener(ServiceEvent.POST_ALBUM_SERVICE, onPostAlbumServiceChange_handler, false, 0, true);
			insertPhotos.addEventListener(InsertPhotoEvent.INSERT_SINGLE_PHOTO, onInsertNetPhoto_handler, false, 0, true);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
				
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.GET_ALBUMS, ApplicationFacade.GET_ALBUM_PHOTOS, ApplicationFacade.UPLOAD_PHOTO, ApplicationFacade.PHOTO_FILE_DATA]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.GET_ALBUMS:
					var list:Vector.<AlbumVO> = notification.getBody() as Vector.<AlbumVO>;
					if(list && list.length)
					{
						insertPhotos.getGetAlbumViewStack.selectedChild = insertPhotos.albumView;
						if(insertPhotos.getGetAlbumAbleList)
						{
							insertPhotos.getGetAlbumAbleList.dataProvider = AlbumVO.VectorToArray(list);
						}	
					}					
					break;
				case ApplicationFacade.GET_ALBUM_PHOTOS:
					var albumPhotosVO:AlbumPhotosVO = notification.getBody() as AlbumPhotosVO;
					if(albumPhotosVO)
					{
						insertPhotos.getGetAlbumViewStack.selectedChild = insertPhotos.photoView;
						if(insertPhotos.getGetAlbumPhotoList)
						{
							insertPhotos.getGetAlbumPhotoList.dataProvider = PhotoVO.VectorToArray(albumPhotosVO.list);
						}						
					}
					break;
				case ApplicationFacade.UPLOAD_PHOTO:
					insertPhotos.uploadPreviewViewStack.selectedIndex = 1;
					var photo:PhotoVO = notification.getBody() as PhotoVO;
					insertPhotos.previewUploadedPhotoCanvas.photoSource = photo.imageuri;
					break;	
				case ApplicationFacade.PHOTO_FILE_DATA:
					insertPhotos.uploadPreviewViewStack.selectedIndex = 0;
					uploadPhotoVO = notification.getBody() as UploadPhotoVO;
					insertPhotos.uploadPhotoTextInput.text = uploadPhotoVO.nativePath;
					insertPhotos.uploadPreview.source = uploadPhotoVO.image;
					break;		
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onCloseWindow_handler(e:Event = null):void
		{
			draftProxy = null;
			serviceProxy = null;
			
			facade.removeMediator(NAME);
			facade.removeCommand(ApplicationFacade.OPEN_GET_PHOTO);
			insertPhotos.removeEventListener(InsertPhotos.CLOSE_TYPED_WINDOW, onCloseWindow_handler);
			insertPhotos.removeEventListener(InsertPhotos.DOUBLE_CLICK_GET_ALBUMABLE, onGetAlbumPhoto_handler);
			insertPhotos.removeEventListener(InsertPhotos.UPLOAD_PHOTO, onPostAlbumPhoto_handler);
			insertPhotos.removeEventListener(InsertPhotos.GET_IMAGE_FILE, onOpenForImageChoose_handler);
			insertPhotos.removeEventListener(InsertPhotos.INSERT_SERVICE_PHOTOS, onInsertServicePhoto_handler);
			insertPhotos.removeEventListener(ServiceEvent.GET_ALBUM_SERVICE, onGetAlbumServiceChange_handler);
			insertPhotos.removeEventListener(ServiceEvent.POST_ALBUM_SERVICE, onPostAlbumServiceChange_handler);
			insertPhotos.removeEventListener(InsertPhotoEvent.INSERT_SINGLE_PHOTO, onInsertNetPhoto_handler);
			
			WindowManager.unRegisterWindow(insertPhotos);
			insertPhotos.close();
		}
		
		//net
		
		private function onInsertNetPhoto_handler(e:InsertPhotoEvent):void
		{
			var imgCode:String = e.htmlcode;
			sendNotification(ApplicationFacade.INSERT_PHOTOS_HTML, imgCode);
			onCloseWindow_handler();
		}
		
		//get
		
		private function onGetAlbumServiceChange_handler(e:ServiceEvent):void
		{
			getPhotoService = ServiceFactory.getGetAlbumService(e.service, serviceProxy, insertPhotos, servicePool);
			getPhotoService.getAlbums();
		}
		
		private function onGetAlbumPhoto_handler(e:Event):void
		{
			if(getPhotoService)
			{
				var album:AlbumVO = insertPhotos.getGetAlbumAbleList.selectedItem as AlbumVO;
				getPhotoService.getAlbumPhotos(album);
			}
		}
		
		//post
		
		private function onPostAlbumServiceChange_handler(e:ServiceEvent):void
		{
			putPhotoService = ServiceFactory.getPostAlbumService(e.service, serviceProxy, draftProxy, insertPhotos, servicePool);
		}
		
		private function onOpenForImageChoose_handler(e:Event):void
		{
			sendNotification(ApplicationFacade.OPEN_GET_PHOTO);
		}
		
		private function onPostAlbumPhoto_handler(e:Event):void
		{
			if(putPhotoService)
			{
				putPhotoService.uploadPhotos(uploadPhotoVO);
			}
		}
		
		private function onInsertServicePhoto_handler(e:Event):void
		{
			var code:String = "";
			if(insertPhotos.selectedGetAlbumAble && insertPhotos.selectedGetAlbumAble.length > 0)
			{
				for each(var p:PhotoVO in insertPhotos.selectedGetAlbumAble)
				{
					code += HTMLCoder.HTMLImgCode(p.imageuri) + "<br>";
				}
			}
			
			sendNotification(ApplicationFacade.INSERT_PHOTOS_HTML, code);
		}
	}
}